package com.google.code.googlefight;

import java.io.IOException;
import java.util.Locale;

import org.json.JSONException;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;

import com.google.code.googlefight.fetch.GoogleSearch;
import com.google.code.googlefight.fetch.GoogleSearchException;
import com.google.code.googlefight.fetch.GoogleSearchImplAPI;
import com.google.code.googlefight.fetch.GoogleSearchImplDirect;

public class MainActivity extends Activity {

	private AnimatedFightView canvasView;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		//erstelle neues view und füge es dem platzhalter hinzu
		canvasView = (AnimatedFightView) findViewById(R.id.canvas);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}
	
	public void startFight(View view) {
		String fightWord1 = ((EditText) findViewById(R.id.edit_fight1)).getText().toString();
		String fightWord2 = ((EditText) findViewById(R.id.edit_fight2)).getText().toString();
		
		new GoogleSearchTask().execute(fightWord1, fightWord2);
	}
	
	public void onResume() {
		super.onResume();
		
		canvasView.startAnimationThread();
	}
	
	public void onPause() {
		super.onPause();
		
		canvasView.stopAnimationThread();
	}
	
	class GoogleSearchTask extends AsyncTask<String, Void, Long[]> {

	    private GoogleSearchException exception;
	    private String[] words;
	    
	    protected Long[] doInBackground(String... words) {
	    	this.words=words;
	    	
	    	try {
	    		GoogleSearch helper = new GoogleSearchImplDirect();
	    		
	    		long fightResult1 = helper.getSearchResults(words[0], Locale.getDefault());
				long fightResult2 = helper.getSearchResults(words[1], Locale.getDefault());
				
				return new Long[]{fightResult1, fightResult2};
			} catch(GoogleSearchException e) {
				this.exception=e;
				return null;
			}
	    }

	    protected void onPostExecute(Long[] results) {
	    	if(results==null) {
	    		AlertDialog.Builder dlgBuilder=new AlertDialog.Builder(MainActivity.this)
	    			.setTitle(R.string.error_title);
	    		
	    		if(this.exception.getCause() instanceof IOException) {
	    			dlgBuilder.setMessage(R.string.error_io);
	    		} else if(this.exception.getCause() instanceof JSONException) {
	    			dlgBuilder.setMessage(R.string.error_parsing);
	    		} else {
	    			dlgBuilder.setMessage(R.string.error_other);
	    		}
	    		
	    		AlertDialog dlg = dlgBuilder.create();
	    		dlg.show();
	    		
	    	} else {
	    		canvasView.animateResults(words, results);
	    	}
	    }
	}
	
	public boolean onOptionsItemSelected(MenuItem item) {
	    // Handle item selection
	    switch (item.getItemId()) {
	    case R.id.action_about:
	        showAbout();
	        return true;
	    default:
	        return super.onOptionsItemSelected(item);
	    }
	}
	
	private void showAbout() {
		Intent intent = new Intent(this, AboutActivity.class);
		startActivity(intent);
	}
}